package com.shiguo365.market.presenter.implement;

import android.app.Activity;
import android.app.Fragment;
import android.support.annotation.NonNull;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;
import com.shiguo365.market.application.MyApplication;
import com.shiguo365.market.display.util.ToastUtils;
import com.shiguo365.market.display.view.IIndexView;
import com.shiguo365.market.display.view.IProductListView;
import com.shiguo365.market.presenter.contract.IProductListPresenter;

import java.util.List;

import biz.yfsoft.api.ae.api.AEClient;
import biz.yfsoft.api.ae.api.AEParam;
import biz.yfsoft.api.ae.api.BaseAECallback;
/**
 * Created by caomenglong on 16/7/30.
 */
public class ProductListPresenter implements IProductListPresenter{

    private static final String TAG = "ProductListPresenter";
    private final Activity activity;
    private final IProductListView mView;

    public ProductListPresenter(@NonNull Activity activity, @NonNull IProductListView view) {
        this.activity = activity;
        this.mView = view;
    }

    @Override
    public void getProlistByTag(String tagCode, int page,int size) {
        AEClient _client = new AEClient("shiguo.data.getProlistByTag");
        final AEParam aeParam =new AEParam();
        aeParam.set("tag",tagCode);
        aeParam.set("page",page);
        aeParam.set("size",size);
        _client.call(aeParam,new BaseAECallback() {

            @Override
            public void onSuccess(List<JSONObject> list) {
                if(list.size()>0){
                    Log.e(TAG,"一条"+aeParam.toJSONString());
                    Log.e(TAG,list.toString());
                    mView.onGetProlistByTagOk(list);
                }else{
                    Log.e(TAG,"没有了？"+aeParam.toJSONString());
                    Log.e(TAG,list.toString());
                    ToastUtils.with(MyApplication.getAppContext()).show("已经全部展示了");
                    mView.onGetProlistByTagFinish();
                }
            }

            @Override
            public void onError(String errno, String message) {
                super.onError(errno,message);
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {
                Log.e(TAG,"onFinally");
                mView.onGetProlistByTagFinish();
            }

        });
    }

    @Override
    public void searchProlistByName(String name, int page, int size) {
        AEClient _client = new AEClient("shiguo.data.searchProlistByName");
        final AEParam aeParam =new AEParam();
        aeParam.set("name",name);
        aeParam.set("page",page);
        aeParam.set("size",size);
        _client.call(aeParam,new BaseAECallback() {

            @Override
            public void onSuccess(List<JSONObject> list) {
                if(list.size()>0){
                    mView.onGetProlistByTagOk(list);
                }else{
                    ToastUtils.with(MyApplication.getAppContext()).show("已经全部展示了");
                    mView.onGetProlistByTagFinish();
                }
            }

            @Override
            public void onError(String errno, String message) {
                super.onError(errno,message);
                Log.e(TAG,"onFailure ......." + errno);
            }

            @Override
            public void onFinally(String s) {
                Log.e(TAG,"onFinally");
                mView.onGetProlistByTagFinish();
            }

        });
    }
}
